---
--- Created by mick.
--- DateTime: 17-11-22 下午12:07
---

local _OBJ = {}
local cookie = require "frame.cookie"
--local config = require "config"
function _OBJ.init(self)
    cookie_t = cookie:get_table()
    if not cookie_t["wlsession"] then
        ngx.ctx.sess_id = lfc.mt_sn();
        cookie:add("wlsession", ngx.ctx.sess_id)
    else
        ngx.ctx.sess_id = cookie_t["wlsession"]
    end
    local redis_client = factory:redis()
    if not redis_client then
        ngx.say("启用session需要redis支持")
        ngx.exit(200);
    end


end

function _OBJ.set(self,key, value)
    local sess_table = {}
    sess_table[key] = value
    local json = cjson.encode(sess_table)
    return factory:redis():setex(ngx.ctx.sess_id,86400,json )

end


function _OBJ.add(self,key,value)
    local sess_table = self:get_table()
    sess_table[key] = value
    local json = cjson.encode(sess_table)
    return factory:redis():setex(ngx.ctx.sess_id,86400,json )

end

function _OBJ.get_table(self)
    local sess_table = {}
    json = factory:redis():get(ngx.ctx.sess_id)
    if  json and json ~= ngx.null then
        sess_table = cjson.decode(json)
    end
    return sess_table
end



return _OBJ